package eraserhead.boundary;

import java.awt.Canvas;
import java.awt.Graphics;

import eraserhead.entity.Decision;
import eraserhead.entity.Edge;

/**
 * The drawing canvas.
 * Original code from Kevin's A1 Implementation
 * Some modifications made by Cian.
 * 
 * @author Cian
 * @author Kevin
 * @author Quanquan
 */
public class DecisionLinesCanvas extends Canvas {
	private static final long serialVersionUID = -3943304806897564687L;
	Decision model;
	int[] edgeArray;
	public final int margin = 30;
	
	boolean canAddEdge = false;

	/**
	 * Makes sure the canvas sees the model, sets up the canvas size.
	 * @param model Decision associated with this {@link DecisionLinesCanvas}
	 */
	public DecisionLinesCanvas(Decision model)
	{
		super();
		this.model = model;
		//this.setSize(401, 201);
		this.edgeArray = new int[model.getLines().size()];
	}
	
	/**
	 * Paints the lines, and on redraw, paints every edge in the model.
	 */
	@Override
	public void paint(Graphics g)
	{
		int height = getHeight();
		int width = getWidth();
		int r = this.model.getLines().size();
		int s = (width - margin * 2) / (r - 1);
		
		// r starts at 0!!!  so x[0] through x[r-1]
		
		for (int i = 0; i < r; i++) {
			g.drawLine(margin + s * i, 0, margin + s * i, height);
			edgeArray[i] = margin + s * i;
		}
		
		/*while (r > 0) {
			g.drawLine(s*r, 0, s*r, 400);
			edgeArray[r-1] = s*r;
			r--;
		}*/
		
		for (int i = 0; i < model.getEdges().size(); i++)
		{
			g.drawLine(edgeArray[model.getEdges().get(i).getLeft()], 
					model.getEdges().get(i).getHeight(), 
					edgeArray[model.getEdges().get(i).getRight()], 
					model.getEdges().get(i).getHeight());
		}	
	}
	
	/**
	 * Determines if the user can add an {@link Edge}
	 * @return True if user can add {@link Edge} false otherwise
	 */
	public boolean getCanAddEdge() {
		return canAddEdge;
	}

	/**
	 * Sets the ability to add an {@link Edge}
	 * @param canAddEdge True for the ability to add an {@link Edge}
	 */
	public void setCanAddEdge(boolean canAddEdge) {
		this.canAddEdge = canAddEdge;
	}
}
